package com.canbot.u05.activity.training;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;

public class TrainingGridViewAdapter extends BaseAdapter {

        private String[] list;

        private String[] listId;

        private LayoutInflater inflater;

        private int selectPos = -1;

        private Context context;

        public TrainingGridViewAdapter(String[] list, String[] listId, Context context) {
                this.list = list;
                this.listId=listId;
                this.context=context;
                inflater = LayoutInflater.from(context);
        }

        public void updateSelect(int position) {
                selectPos = position;
                notifyDataSetChanged();
        }


        @Override
        public int getCount() {
                return list.length;
        }

        @Override
        public Object getItem(int arg0) {
                return arg0;
        }

        @Override
        public long getItemId(int arg0) {
                return arg0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup arg2) {
                ViewHolder viewHolder;
                if (convertView == null) {
                        convertView = inflater.inflate(R.layout.action_gride_item, null);
                        viewHolder = new ViewHolder();
                        viewHolder.title = (TextView) convertView.findViewById(R.id.text_item);
                        viewHolder.image = (ImageView) convertView.findViewById(R.id.imgge_state);
                        convertView.setTag(viewHolder);
                }
                else {
                        viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.title.setText(list[position]);
                if (position == selectPos) {
                        viewHolder.image.setVisibility(View.VISIBLE);
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, listId[position]);
                        ToastTools.newInstance(context).showText(context.getString(R.string.action_preview));
                }
                else {
                        viewHolder.image.setVisibility(View.INVISIBLE);
                }
                return convertView;
        }

        private class ViewHolder {

                public TextView title;

                public ImageView image;
        }

}
